/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.persistency;

import org.aroundme.model.Location;
import org.aroundme.utility.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;
/**
 * Persistency class responsible for retrieving and saving
 * Location-objects to the DB
 */
public class LocationManager {
	
	public LocationManager(){
		
	}
	
	/**
	 * Saves a Location object to the DB
	 * @param l Location object
	 */
	public void saveLocation(Location l){
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		s.beginTransaction();
		try{
			s.saveOrUpdate(l);
			s.beginTransaction().commit();
		}catch(HibernateException e){
			s.getTransaction().rollback();
			throw e;
		}
	}
	
	/**
	 * Retrieves a Location object with the specified cellid
	 * @param cellid Cellid of the object to be retrieved
	 * @return Location object representing the location with the cellid indicated by param "cellid"
	 */
	public Location getLocationWithCellId(String cellid){
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		Location result = null;
		s.beginTransaction();
		try{
			result = (Location)s.createQuery("from Location where cellid=?").setText(0, cellid).uniqueResult();
			s.getTransaction().commit();
		}catch (HibernateException e){
			s.getTransaction().rollback();
			throw e;
		}
		return result;
	}
	
}
